package com.ad.pet.common.data.utils.ui;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.ad.pet.R;
import com.ad.pet.app.Navigation;

/**
 * Created by z on.
 * Des:
 * Date: 2017/3/13
 */

public class DialogUtil {

    public static AlertDialog createDialog(Activity activity, String title, String message, boolean iscancelabe
            , String button1Str, Dialog.OnClickListener button1Listener,
                                           String button2Str, Dialog.OnClickListener button2Listener) {

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }
        AlertDialog alertDialog = builder
                .setCancelable(iscancelabe)
                .setMessage(message)
                .setNegativeButton(button1Str, button1Listener)
                .setPositiveButton(button2Str, button2Listener).create();
//        alertDialog.show();
        return alertDialog;
    }

    public static AlertDialog createDialogNoShow(Activity activity, String title, String message, boolean isCancelable
            , String button1Str, Dialog.OnClickListener button1Listener,
                                                 String button2Str, Dialog.OnClickListener button2Listener) {
        AlertDialog alertDialog = new AlertDialog.Builder(activity)
                .setCancelable(isCancelable)
                .setTitle(title)
                .setMessage(message)
                .setNegativeButton(button1Str, button1Listener)
                .setPositiveButton(button2Str, button2Listener).create();
        return alertDialog;
    }

    public static AlertDialog createDialog(Activity activity, String title, String message,
                                           String button2Str, Dialog.OnClickListener button2Listener) {
        return createDialog(activity, title, message, false, activity.getString(R.string.cancel), null, button2Str, button2Listener);
    }

    public static AlertDialog createDialog(Activity activity, String message,
                                           String button2Str, Dialog.OnClickListener button2Listener) {
        return createDialog(activity, "", message, false, activity.getString(R.string.cancel), null, button2Str, button2Listener);
    }

    public static AlertDialog createDialogNoShow(Activity activity, String title, String message,
                                                 String button2Str, Dialog.OnClickListener button2Listener) {
        return createDialogNoShow(activity, title, message, false, activity.getString(R.string.cancel), null, button2Str, button2Listener);
    }

    public static AlertDialog createNotifyDialog(Activity activity, String title, String message,
                                                 String buttonStr, Dialog.OnClickListener button2Listener) {
        return new AlertDialog.Builder(activity)
                .setTitle(title)
                .setMessage(message)
                .setNegativeButton(buttonStr, button2Listener).create();
    }

    public static AlertDialog createNotifyDialog(Activity activity, String title, String message,
                                                 String buttonStr) {
        return createNotifyDialog(activity, title, message, buttonStr, null);
    }

    public static AlertDialog createDialog(Activity activity, String title, String message, boolean iscancelabe, View v
            , String button1Str, Dialog.OnClickListener button1Listener,
                                           String button2Str, Dialog.OnClickListener button2Listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                .setCancelable(iscancelabe);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }
        AlertDialog alertDialog = builder
                .setCancelable(iscancelabe)
                .setView(v)
                .setMessage(message)
                .setNegativeButton(button1Str, button1Listener)
                .setPositiveButton(button2Str, button2Listener).create();
        alertDialog.show();
        return alertDialog;
    }

    public static AlertDialog createDialog(Activity activity, String title, String message, View v,
                                           String button2Str, Dialog.OnClickListener button2Listener) {
        return createDialog(activity, title, message, false, v, activity.getString(R.string.cancel), null, button2Str, button2Listener);
    }

    public static AlertDialog createDialog(Activity activity, String title, View v,
                                           String button2Str, Dialog.OnClickListener button2Listener) {
        return createDialog(activity, title, "", false, v, activity.getString(R.string.cancel), null, button2Str, button2Listener);
    }

    public static AlertDialog createDialog(Activity activity, String title, String message, String button1Str, Dialog.OnClickListener button1Listener
            , String button2Str, Dialog.OnClickListener button2Listener,
                                           String button3Str, Dialog.OnClickListener button3Listener) {
        return new AlertDialog.Builder(activity)
                .setCancelable(false)
                .setTitle(title)
                .setMessage(message)
                .setNegativeButton(button1Str, button1Listener)
                .setNeutralButton(button2Str, button2Listener)
                .setPositiveButton(button3Str, button3Listener).create();
    }


    public static ProgressDialog createProgressDialog(Activity activity, String title, String message, boolean cancelable) {
        ProgressDialog progressDialog = new ProgressDialog(activity);
        if (!TextUtils.isEmpty(title)) {
            progressDialog.setTitle(title);
        }
        progressDialog.setMessage(message);
        progressDialog.setCancelable(cancelable);
        return progressDialog;

    }

    public static ProgressDialog createProgressDialog(Activity activity) {
        return createProgressDialog(activity, "", "Loading...", false);
    }

    public static ProgressDialog createProgressDialog(Activity activity, String content) {
        return createProgressDialog(activity, "", content, false);
    }

    public static AlertDialog createListDialog(Activity activity, String title, String[] strings, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity).setItems(strings, listener);
        if (!TextUtils.isEmpty(title)) {
            builder.setTitle(title);
        }
        return builder.create();
    }

    public static AlertDialog createListDialog(Activity activity, String[] strings, DialogInterface.OnClickListener listener) {
        return createListDialog(activity, "", strings, listener);
    }

    public static AlertDialog createListDialog(Activity activity, String[] strings) {
        return createListDialog(activity, "", strings, null);
    }

    public static AlertDialog createSingleChoiceItemsDialog(Activity act, String title,
                                                            String[] items, int checkedItem, final DialogInterface.OnClickListener listener) {
        return new AlertDialog.Builder(act)
                .setTitle(title)
                .setSingleChoiceItems(items, checkedItem, listener).create();

    }
    public static AlertDialog createSingleChoiceItemsDialog(Activity act,
                                                            String[] items, int checkedItem, final DialogInterface.OnClickListener listener) {
        return new AlertDialog.Builder(act)
                .setSingleChoiceItems(items, checkedItem, listener).create();

    }


//    private static OptionsPickerView pvCustomOptions;
//    public static void initSelectValueDialog(Context context, final List<String> options1Items, final OnGetSelectValueLinstener linstener) {
//
//        pvCustomOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
//            @Override
//            public void onOptionsSelect(int i, int i1, int i2, View view) {
//                linstener.onGetValue(i);
//            }
//        }) .setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
//            @Override
//            public void onOptionsSelectChanged(int options1, int options2, int options3) {
//
//            }
//        })       .setContentTextSize(20)//设置滚轮文字大小
//                .setDividerColor(Color.DKGRAY)//设置分割线的颜色
//                .setSelectOptions(0, 1)//默认选中项
//                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                .setBackgroundId(0x66000000) //设置外部遮罩颜色
//                .setSubmitText(context.getString(R.string.confirm))//确定按钮文字
//                .setCancelText(context.getString(R.string.cancel))//取消按钮文字
//                .setTitleText(context.getString(R.string.default_reciver_area))//标题
//                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                .setCyclic(false, false, false)//循环与否
//                .setSelectOptions(1, 1, 1)  //设置默认选中项
//                .setOutSideCancelable(true)//点击外部dismiss default true
//                .build();
//
//        pvCustomOptions.setPicker(options1Items);//添加数据源
//        pvCustomOptions.show();
//    }
    public static void show(ProgressDialog pd) {
        if (pd != null && !pd.isShowing()) {
            pd.show();
        }
    }

    public static void hide(ProgressDialog pd) {
        if (pd != null && pd.isShowing()) {
            pd.dismiss();
        }
    }

    public static AlertDialog createPrivateDialog(final Activity mActivity, String title, Dialog.OnClickListener button2Listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        View view = View.inflate(mActivity,R.layout.item_private_layout,null);
        TextView tvXieYi = view.findViewById(R.id.tv_xieyi);
        SpannableStringBuilder style = new SpannableStringBuilder();
        style.append("        感谢您信任并使用当当DOG！我们依据最新的监管要求修订了《当当DOG隐私政策》和《当当DOG用户协议》，请您在点击同意之前仔细阅读并充分理解相关条款，完善后的内容更有利于帮助您理解我们收集、使用、存储和分享您必要个人信息的情形，未经您同意，我们不会从第三方处获取、共享或向其提供您的信息。我们将不断完善技术和安全管理，保护您的个人信息。当当DOG将一如既往坚守使命，让您和您的宠物得到更好的服务体验！");

        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Navigation.goCommonWebView(mActivity, "", mActivity.getString(R.string.ip_host)+"/index.html#/service/PrivacyPolicy");
            }
        };

        ClickableSpan clickableSpan2 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Navigation.goCommonWebView(mActivity, "", mActivity.getString(R.string.ip_host)+"/index.html#/service/ServicesAgreement");
            }
        };
//        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#0000FF"));
//        style.setSpan(foregroundColorSpan, 28, 37, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//
//        ForegroundColorSpan foregroundColorSpan2 = new ForegroundColorSpan(Color.parseColor("#0000FF"));
//        style.setSpan(foregroundColorSpan2, 41, 60, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(clickableSpan, 36, 47, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(clickableSpan2, 48, 59, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvXieYi.setMovementMethod(LinkMovementMethod.getInstance());
        tvXieYi.setText(style);
        builder.setTitle(title);
        AlertDialog alertDialog = builder
                .setCancelable(false).setView(view)
                .setNegativeButton("不同意", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mActivity.finish();
                    }
                })
                .setPositiveButton("同意并继续", button2Listener).create();
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.GRAY);
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(15);
        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextSize(15);


        WindowManager m = mActivity.getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();  //获取对话框当前的参数值
//        p.height = (int) (d.getWidth() * 1.2);   //高度设置为屏幕的0.3
//        p.width = (int) (d.getWidth() * 0.75);    //宽度设置为屏幕的0.5
        alertDialog.getWindow().setAttributes(p);     //设置生效
        //解决dilaog中EditText无法弹出输入的问题
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //弹出对话框后直接弹出键盘

        return alertDialog;
    }

    /**
     * 输入购物车数量
     * @param mActivity
     * @return
     */
    public static AlertDialog createDialog(final Activity mActivity, String title, Dialog.OnClickListener button2Listener, String text) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        builder.setTitle(title);
        AlertDialog alertDialog = builder
                .setCancelable(false).setView(R.layout.item_add_trans_say)
                .setNegativeButton(mActivity.getString(R.string.cancel), null)
                .setPositiveButton(mActivity.getString(R.string.confirm), button2Listener).create();
        alertDialog.show();
        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.GRAY);
        alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextSize(15);
        alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextSize(15);
        final EditText editText = alertDialog.findViewById(R.id.et_say);
        editText.setText(text);
        editText.setSelection(0, text.length());
        editText.setSelection(0);
        editText.setSelectAllOnFocus(true);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!s.toString().trim().isEmpty()){
                    int min = 1;
                    int max = 99999;
                    int value = Integer.valueOf(s.toString());
                    if (value<min)
                    {
                        editText.setText(String.valueOf(min));

                    }else if(value>max){
                        editText.setText(String.valueOf(max));
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        WindowManager m = mActivity.getWindowManager();
        Display d = m.getDefaultDisplay();  //为获取屏幕宽、高
        android.view.WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();  //获取对话框当前的参数值
        p.height = (int) (d.getWidth() * 0.55);   //高度设置为屏幕的0.3
        p.width = (int) (d.getWidth() * 0.75);    //宽度设置为屏幕的0.5
        alertDialog.getWindow().setAttributes(p);     //设置生效
        //解决dilaog中EditText无法弹出输入的问题
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //弹出对话框后直接弹出键盘
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager inputManager =(InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(editText, 0);
            }
        }, 100);
        return alertDialog;
    }




}
